<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%@include file="/WEB-INF/views/common/header.jsp" %>
    <title>新建兼职人员</title>
</head>
<body>
<%@include file="/WEB-INF/views/common/top.jsp" %>
<%@include file="/WEB-INF/views/common/nav.jsp" %>

<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <div class="show-top-grids">
        <div class="main-grids about-main-grids">
            <h4>新建兼职信息</h4>
            <div class="recommended-info" pane="">

                <hr class="layui-bg-green">
                <form id="createPartForm" class="layui-form layui-form-pane" style="margin-top: 1em;" method="post"
                      action='<c:url value="/part/create"/>'>

                    <div class="layui-form-item">
                        <div class="layui-col-xs12">
                            <label class="layui-form-label">标题</label>
                            <div class="layui-col-xs9">
                                <input type="text" name="title" id="title" placeholder="标题" autocomplete="off"
                                       value="" class="layui-input"/>
                            </div>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">薪资</label>
                            <div class="layui-input-inline">
                                <input type="number" name="salary" id="salary" placeholder="薪资" autocomplete="off"
                                       value="" class="layui-input"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">人数</label>
                            <div class="layui-input-inline">
                                <input type="number" name="peopleNumber" id="peopleNumber"
                                       placeholder="人数" autocomplete="off"
                                       value="" class="layui-input"/>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">开始时间</label>
                            <div class="layui-input-inline">
                                <input type="text" readonly="readonly" name="startTime" id="startTime"
                                       placeholder="开始时间" autocomplete="off"
                                       value="" class="layui-input"/>
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label">结束时间</label>
                            <div class="layui-input-inline">
                                <input type="text" readonly="readonly" name="endTime" id="endTime"
                                       placeholder="结束时间" autocomplete="off"
                                       value="" class="layui-input"/>
                            </div>
                        </div>

                    </div>


                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">公司名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="companyName" id="companyName" placeholder="公司名称"
                                       autocomplete="off" value=""
                                       class="layui-input"/>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">兼职类型</label>
                            <div class="layui-input-inline">
                                <select id="partTypeId" name="partTypeId" style="display: inline-block;"
                                        class="layui-input">
                                    <option value="">请选择</option>
                                    <c:forEach var="partType" items="${partTypeVos}">
                                        <option value="${partType.id}">${partType.partTypeName}</option>
                                    </c:forEach>
                                </select>
                            </div>
                        </div>

                    </div>


                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">工作省</label>
                            <div class="layui-input-inline">
                                <select id="province" name="province" tag="province"
                                        style="display: inline-block;"
                                        class="layui-input">
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">工作市</label>
                            <div class="layui-input-inline">
                                <select id="city" name="city" tag="city" style="display: inline-block;"
                                        class="layui-input">
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">工作县</label>
                            <div class="layui-input-inline">
                                <select id="town" name="town" tag="town" style="display: inline-block;"
                                        class="layui-input">
                                    <option value="">请选择</option>
                                </select>

                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">街道</label>
                            <div class="layui-input-inline ">
                                <select id="road" name="road" style="display: inline-block;"
                                        class="layui-input">
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block layui-col-sm6" style="margin-left:0;">
                            <input type="text" name="address" id="address"
                                   placeholder="地址" autocomplete="off"
                                   value="" class="layui-input"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">兼职时间</label>
                        <div class="layui-input-block layui-col-sm6" style="margin-left:0;height: 2em;"
                             id="divContainer">
                            <input type="checkbox" title="星期一" value="1" name="partTimes"/>
                            <input type="checkbox" title="星期二" value="2" name="partTimes"/>
                            <input type="checkbox" title="星期三" value="3" name="partTimes"/>
                            <input type="checkbox" title="星期四" value="4" name="partTimes"/>
                            <input type="checkbox" title="星期五" value="5" name="partTimes"/>
                            <input type="checkbox" title="星期六" value="6" name="partTimes"/>
                            <input type="checkbox" title="星期日" value="7" name="partTimes"/>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <div class="layui-col-xs12">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-col-xs9">
                                <input type="text" name="remark" id="remark" placeholder="备注" autocomplete="off"
                                       value="" class="layui-input"/>
                            </div>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <div class="layui-col-xs12">
                            <label class="layui-form-label">详情</label>
                            <div class="layui-col-xs9">
                                <textarea name="detail" id="detail" placeholder="详情" class="layui-textarea"></textarea>
                            </div>
                        </div>
                    </div>


                    <div class="layui-col-md4 layui-col-md-offset4">
                        <div class="grid-demo">
                            <button class="layui-btn" type="submit" id="btn_create_part">添加</button>
                        </div>
                    </div>

                </form>
            </div>
        </div>
    </div>

</div>
</body>

<script type="application/javascript">

    /*导航栏参数*/
    var $nav_a = $("#ul_nav a[href='/part/create']");
    $nav_a.parent().addClass("active");
    $nav_a.parent().parent().css("display", "block");


    layui.use(['layer', 'laydate'], function () {
        window.layer = layui.layer;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#startTime',
            min: 0
        });
        laydate.render({
            elem: '#endTime',
            min: 0
        });
    });


    $(function () {
        /*绑定省*/
        initSelect("province");
        /*修改checkbox 样式*/
        $("#divContainer :checkbox").each(function () {
            var title = $(this).attr("title");
            var funcType = $(this).attr("tag-index");
            var div = $("<div class=\"layui-unselect layui-form-checkbox\" func-type=" + funcType + " lay-skin=\"primary\">"
                + "<span>" + title + "</span><i class=\"layui-icon\"></i></div>");

            $(div).click(function () {
                var che = $(this).prev().prop("checked");
                $(this).prev().prop("checked", !che);
                if (!che) {
                    $(this).addClass("layui-form-checked");
                } else {
                    $(this).removeClass("layui-form-checked");
                }
            });
            $(this).after(div);
        });

        $("#province").change(function () {
            initSelect("city", $(this).val());
        });
        $("#city").change(function () {
            initSelect("town", $(this).val());
        });
        $("#town").change(function () {
            initArea("road", $(this).val());
        });


        window.fault = false;
        $("#createPartForm").validate({
            onfocusout: false,
            onkeyup: false,
            onclick: false,
            submitHandler: function () {


                if ($("input[name='partTimes']:checked").length == 0) {
                    $("#divContainer").addClass("layui-form-danger").focus();
                    window.layer.msg("请选择兼职时间", {icon: 5, anim: 6});
                    return false;
                }
                var formData = $('#createPartForm').serializeObject();
                formData = JSON.stringify(formData);
                $.ajax({
                    url: '<c:url value="/part/create"/>',
                    dataType: 'json',
                    type: 'post',
                    data: formData,
                    contentType: "application/json",
                    error: function () {
                    },
                    success: function (data) {
                        if (data.code !== "200") {
                            window.layer.msg(data.message, {icon: 5, anim: 6});
                        } else {
                            window.layer.msg("创建成功！", {icon: 6, anim: 6});
                            window.location.href = '<c:url value="/user/list"/>'
                        }
                    }
                });
            },
            rules: {
                title: {
                    required: true
                },
                salary: {
                    required: true
                },
                peopleNumber: {
                    required: true
                },
                startTime: {
                    required: true
                }
                , endTime: {
                    required: true
                }
                , companyName: {
                    required: true
                }
                , partTypeId: {
                    required: true
                }
                , province: {
                    required: true
                }
                , city: {
                    required: true
                }
                , town: {
                    required: true
                }
                , address: {
                    required: true
                }
                , partTimes: "required"

            },
            messages: {
                title: "标题不能为空"
                , salary: "薪资不能为空"
                , peopleNumber: "人数不能为空"
                , startTime: "开始日期"
                , endTime: "结束日期"
                , companyName: "公司名称不能为空"
                , partTypeId: "请选择兼职类型"
                , province: "省信息请选择"
                , city: "市信息请选择"
                , town: "县信息请选择"
                , address: "详情请选择"
                , partTimes: "请选择兼职时间"
            },
            showErrors: function (errorMap, errorList) {
                window.fault = this.numberOfInvalids() > 0;
                this.defaultShowErrors();
            },
            errorPlacement: function (error, element) {
                if (window.fault) {
                    window.fault = false;
                    $(element).addClass("layui-form-danger").focus();
                    window.layer.msg($(error).text(), {icon: 5, anim: 6});
                }
            }
        });
    });


    function initSelect(name, parentCode) {
        var container = $("#" + name);
        $(container).empty();
        var tag = $(container).attr("tag");
        $(container).append("<option value=''>请选择</option>");
        $.ajax({
            url: '/static/json/list.json'
            , type: 'GET'
            , dataType: "json"
            , success: function (data) {
                for (var code in data) {
                    if (tag == "province" && code % 1e4 == 0) {
                        $(container).append("<option value='" + code + "'>" + data[code] + "</option>");
                    } else if (tag == "city" && code % 1e2 == 0) {
                        var preCode = parseInt(code / 1e4);
                        var preParentCode = parseInt(parentCode / 1e4);


                        if (preCode == preParentCode) {
                            if (code != parentCode || ["50", "31", "11", "12"].indexOf(preCode + "") > -1) {
                                $(container).append("<option value='" + code + "'>" + data[code] + "</option>");
                            }
                        }
                    } else if (tag == "town") {
                        if (code % 1e2 != 0) {
                            var preCode = parseInt(code / 1e2);
                            var preParentCode = parseInt(parentCode / 1e2);
                            if (preCode == preParentCode) {
                                $(container).append("<option value='" + code + "'>" + data[code] + "</option>");
                            } else {
                                if (parseInt(preCode / 10) == parseInt(preParentCode / 10)) {
                                    $(container).append("<option value='" + code + "'>" + data[code] + "</option>");
                                }
                            }
                        }
                    }

                }
            }
        });
    }

    function initArea(name, code) {
        var container = $("#" + name);
        $(container).empty();
        $(container).append("<option value=''>请选择</option>");
        if (code == "") {
            return true;
        }
        var url = "/static/json/town/" + code + ".json";
        $.ajax({
            url: url
            , type: 'GET'
            , dataType: "json"
            , success: function (data) {
                for (var code in data) {
                    $(container).append("<option value='" + code + "'>" + data[code] + "</option>");
                }
                $(container).append("<option value='999999'>其他</option>");
            }
        });
    }

</script>


</html>
